* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file runs additional regressions for export market outcomes.

* Setup
use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\uhdi_subset.dta", clear
rename cvrnr cvrnr_uhdi

* We only consider exports in a first step
keep if ie==2
sort senr aar 
merge m:1 senr aar using "D:\Data\Workdata\703989\fjsd3989\STATA FILES\se_cvr_yearly.dta" 

* Compare cvrnr_uhdi with cvrnr in se-cvrnr key file
gen temp=0
replace temp=1 if cvrnr == cvrnr_uhdi & _merge==3
tab temp if _merge==3

keep if _merge==3
drop temp cvrnr_uhdi senr _merge

drop enhedskode ie mgd statistikkode vgt

order cvrnr aar vare land
sort cvrnr aar vare land

rename cvrnr cvrnrA

rename vare vareA
rename aar aarA
rename land landA
rename vrd vrdA

gen byte notnumeric=real(vareA)==.
tab notnumeric
drop if notnumeric==1
destring vareA, replace
rename vareA cn8A
drop if vrdA<=0

rename vrdA vaerdi
rename cn8A vare
rename cvrnrA cvrnr
rename aarA aar

collapse (sum) vaerdi, by(cvrnr aar vare)

rename cvrnr cvrnrT
rename aar aarT
rename vare vareT

merge m:1 cvrnrT using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_did_jc_multi1865.dta", keep(match) nogen

rename vareT cn8T
rename vaerdi vrdT

*Lump all targets together
rename cvrnrT cvrnrT_ori
merge m:1 cvrnrA aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_did_jc_largestT_1865.dta", keepusing(cvrnrT) keep(match) nogen
collapse (sum) vrdT, by(cvrnrT aarT cvrnrA aar)

rename aarT aarA

tempfile temp_T_exports
save `temp_T_exports.dta', replace

********************************************************************************
* Main data
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\regression_data_export.dta", clear

merge 1:1 cvrnrA aarA aar using `temp_T_exports.dta', keepusing(vrdT) keep(match master) gen(_mergeT)

gen p = (p1==1 | p2==1 | p3==1)
replace vrdT = 0 if vrdT==. & aar!=.
egen sumvrdT = sum(vrdT) if p==0 & aar!=., by(cvrnrA aar)
egen meansumvrdT = mean(sumvrdT), by(cvrnrA aar)

preserve
keep if aar!=.
keep cvrnrA aar
duplicates drop
rename aar aar_no
tempfile temp1
save `temp1.dta', replace

restore
merge m:1 cvrnrA using `temp1.dta', nogen

gen aar_drop1 = aar_no if meansumvrdT>0 & aar==aar_no
gen aar_keep1 = aar_no if meansumvrdT==0 & aar==aar_no

foreach x in aar_drop1 aar_keep1{
	egen mean`x' = mean(`x'), by(cvrnrA)
	}

* Figure A.15(c),(d)
foreach x in lnnmark lnnprod {	
did_imputation `x' group_cvrnr aarA aar3 if minemployee_cvrnr>=5 & ((acquirer==1 & meanaar_keep1!=.) | acquirer==0) & _mergedist==3, fe(group_cvrnr aarA) hor(0/2) pretrend(4) tol(0.0001) maxit(250) autosample cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA15.txt", bdec(4) sdec(4) nocons nonotes 
}

foreach x in lnnprodpermark {	
did_imputation `x' group_cvrnr aarA aar3 if minemployee_cvrnr>=5 & ((acquirer==1 & max_merge==3 & meanaar_keep1!=.) | acquirer==0) & _mergedist==3, fe(group_cvrnr aarA) hor(0/2) pretrend(4) tol(0.0001) maxit(250) autosample cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA15.txt", bdec(4) sdec(4) nocons nonotes
}

foreach x in p90_ldistw {	
did_imputation `x' group_cvrnr aarA aar3 if minemployee_cvrnr>=5 & ((acquirer==1 & meanaar_keep1!=.) | acquirer==0) & _mergedist==3, fe(group_cvrnr aarA) hor(0/2) pretrend(4) tol(0.0001) maxit(250) autosample cluster(ind)
outreg2 using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\table_figureA15.txt", bdec(4) sdec(4) nocons nonotes
}